package com.skycong.hello77.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import com.skycong.hello77.R;
import com.skycong.hello77.entity.ApkEntity;
import com.skycong.hello77.viewmodel.ProductCustomerView;
import com.skycong.hello77.viewmodel.ProductDetailView;

import java.util.ArrayList;
import java.util.List;

import static android.content.ContentValues.TAG;

public class RecyclerAdapter extends RecyclerView.Adapter {




     private Context context;
     private  RecyclerView.ViewHolder holder;
     private List<ApkEntity> datatoAdap = new ArrayList<ApkEntity>();
     LayoutInflater inflater;

     public RecyclerAdapter(Context context){
         inflater=LayoutInflater.from(context);

     }
     public RecyclerAdapter(Context context,List<ApkEntity> dataList){
         this.inflater = LayoutInflater.from(context);
         this.datatoAdap = dataList;
     }
     public void addList(List<ApkEntity> dataList){
         datatoAdap.addAll(dataList);
         Log.i("i","dd"+dataList);
     }


   //为Item创建视图
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
         Log.i("i","onCreateViewHolder执行了");
          switch (viewType){
              case ApkEntity.TYPE_CUSTOM:{
                  return new ProductCustomerView(inflater.inflate(R.layout.recycler_custom,parent,false));
              }
              case ApkEntity.TYPE_PRODUCT:{
                  return new ProductDetailView(inflater.inflate(R.layout.recycler_product,parent,false));
              }
              default:
                  return new ProductCustomerView(inflater.inflate(R.layout.recycler_custom,parent,false));

          }

    }
    //将数据绑定在item的视图上
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Log.i(TAG, "onBindViewHolder: "+holder.getClass());
        int viewType = getItemViewType(position);
        Log.i("i","viewType"+viewType);
        switch (viewType){
            case ApkEntity.TYPE_CUSTOM:{
                ProductCustomerView pro = (ProductCustomerView) holder;
                pro.cust(datatoAdap.get(position));
                Log.i(TAG, "ProductCustomerView: "+ProductCustomerView.class);
                Log.i("i","po"+datatoAdap.get(position));
                Log.i("i","do"+datatoAdap);
                break;
            }
            case ApkEntity.TYPE_PRODUCT:{
                Log.i(TAG, "ProductDetailView: "+ProductDetailView.class);
                ProductDetailView pro = (ProductDetailView) holder;
                pro.detail(datatoAdap.get(position));
                break;

            }
        }

    }

    @Override
    public int getItemCount() {

        Log.i("i","size"+datatoAdap.size());
        return datatoAdap.size();
    }
    //根据不同的位置，加载不同的布局
    @Override
    public int getItemViewType(int position) {
         Log.i("i","这里执行到了 真的");
        Log.i("i","得到的type"+datatoAdap.get(position).getType());
        return datatoAdap.get(position).getType();//得到type
    }
}
